//
//  SystemSetViewController.m
//  EducationOID
//
//  Created by slz on 2017/6/27.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "SystemSetViewController.h"
#import "OIDSystemSettingManager.h"

@protocol SystemSettingCellDelegate <NSObject>

- (void)systemSetCellClickAtIndex:(NSInteger)clickIndex groupIndex:(NSInteger)groupIndex andSwitchState:(BOOL)isOn;

@end

@interface SystemSetViewController ()<SystemSettingCellDelegate>

@end

@interface SystemSettingCell : UIView

@property (nonatomic,strong) UISwitch *settingSwitch;
@property(nonatomic,strong) UILabel *titleLabel;
@property(nonatomic,strong) UIView *lineView;
@property(nonatomic,assign) NSInteger index;
@property(nonatomic,assign) NSInteger groupIndex;

@property(nonatomic,weak) id<SystemSettingCellDelegate> delegate;

@end

@implementation SystemSettingCell

-(instancetype)init{
    self = [super init];
    if (self){
        _titleLabel = [UILabel new];
        _titleLabel.fontSize = 14;
        _titleLabel.textColor = kDark_TextColor_Default;
        [self addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(11);
            make.centerY.mas_equalTo(self.mas_centerY);
        }];
        
        _lineView = [UIView new];
        _lineView.backgroundColor = kLineColor_Gray_Default;
        [self addSubview:_lineView];
        [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(10);
            make.right.mas_equalTo(self.mas_right).offset(-10);
            make.height.mas_equalTo(0.5);
            make.bottom.mas_equalTo(self.mas_bottom);
        }];
        
        _settingSwitch = [UISwitch new];
        [_settingSwitch addTarget:self action:@selector(switchState:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:_settingSwitch];
        [_settingSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.mas_right).offset(-11);
            make.centerY.mas_equalTo(self.mas_centerY);
        }];
        
    }
    return self;
}

- (void)switchState:(UISwitch *)settingSwich{
    NSLog(@"%d",settingSwich.isOn);
    if ([_delegate respondsToSelector:@selector(systemSetCellClickAtIndex:groupIndex:andSwitchState:)]){
        [_delegate systemSetCellClickAtIndex:_index groupIndex:_groupIndex andSwitchState:settingSwich.isOn];
    }
}

@end

@interface SystemSettingGroupView : UIView
@property(nonatomic,assign) NSInteger groupIndex;
-(instancetype)initWithGroupTitles:(NSArray *)titles andDelegate:(id)delegate;
@end

@implementation SystemSettingGroupView

-(instancetype)initWithGroupTitles:(NSArray *)titles andDelegate:(id)delegate{
    self = [super init];
    if (self){
        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor whiteColor];
        for (int i = 0; i < titles.count; i ++) {
            NSString * title = titles[i];
            SystemSettingCell * cell = [[SystemSettingCell alloc]init];
            cell.index = i;
            cell.delegate = delegate;
            [self addSubview:cell];
            [cell mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.and.right.mas_equalTo(self);
                make.height.mas_equalTo(40);
                make.top.mas_equalTo(self.mas_top).offset(i * 40);
            }];
            cell.titleLabel.text = title;
            cell.lineView.hidden = i == titles.count - 1;
        }
    }
    return self;
}

-(void)setGroupIndex:(NSInteger)groupIndex{
    OIDSystemSettingManager * manager = [OIDSystemSettingManager shareManager];
    for (SystemSettingCell * cell in self.subviews) {
        cell.groupIndex = groupIndex;
        if (cell.groupIndex == 0)
        {
            switch (cell.index)
            {
                case 0:
                    cell.settingSwitch.on = manager.tipVoice;
                    break;
                case 1:
                    cell.settingSwitch.on = manager.copyToBoarde;
                    break;
                case 2:
                    cell.settingSwitch.on = manager.remmenberDuplicate;
                    break;
                case 3:
                    cell.settingSwitch.on = manager.remmenberDuplicate;
                    break;
                case 4:
                    cell.settingSwitch.on = manager.searchMoreInfo;
                    break;
                default:
                    break;
            }
        }else
        {
            switch (cell.index)
            {
                case 0:
                    cell.settingSwitch.on = manager.cameraLight;
                    break;
                case 1:
                    cell.settingSwitch.on = manager.shakeToCamera;
                    break;
//                case 2:
//                    cell.settingSwitch.on = manager.shakeToCamera;
//                    break;
                default:
                    break;
            }
        }
    }
}

@end

@implementation SystemSetViewController
{
    NSArray *_source1;
    NSArray *_source2;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _source1 = @[@"提示音",@"复制到剪贴板",@"记住重复"];
    _source2 = @[@"前灯",@"主页面摇晃快速拍码"];
    
    self.showNavigationBar = YES;
    [self setControllerTitle:@"系统设置"];
    self.view.backgroundColor = [UIColor colorWithRGB:0xE3E3E3];
    UIBarButtonItem * item = [UIBarButtonItem itemWithImage:@"top_back_1" selectImage:nil addTarget:self andAction:@selector(backAction)];
    self.navigationItem.leftBarButtonItem = item;
    [self initGroupViews];
    // Do any additional setup after loading the view from its nib.
}

- (void)initGroupViews{
    SystemSettingGroupView * groupView1 = [[SystemSettingGroupView alloc]initWithGroupTitles:_source1 andDelegate:self];
    groupView1.groupIndex = 0;
    [self.view addSubview:groupView1];
    [groupView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(11);
        make.top.mas_equalTo(self.view.mas_top).offset(20);
        make.right.mas_equalTo(self.view.mas_right).offset(-11);
        make.height.mas_equalTo(_source1.count * 40);
    }];
    
    SystemSettingGroupView * groupView2 = [[SystemSettingGroupView alloc]initWithGroupTitles:_source2 andDelegate:self];
    groupView2.groupIndex = 1;
    [self.view addSubview:groupView2];
    [groupView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(11);
        make.top.mas_equalTo(groupView1.mas_bottom).offset(15);
        make.right.mas_equalTo(self.view.mas_right).offset(-11);
        make.height.mas_equalTo(_source2.count * 40);
    }];

}

-(void)systemSetCellClickAtIndex:(NSInteger)clickIndex groupIndex:(NSInteger)groupIndex andSwitchState:(BOOL)isOn{
    OIDSystemSettingManager * manager = [OIDSystemSettingManager shareManager];
    if (groupIndex == 0)
    {
        switch (clickIndex)
        {
            case 0:
                manager.tipVoice = isOn;
                break;
            case 1:
                manager.copyToBoarde = isOn;
                break;
            case 2:
                manager.remmenberDuplicate = isOn;
                break;
//            case 3:
//                manager.remmenberDuplicate = isOn;
//                break;
//            case 4:
//                manager.searchMoreInfo = isOn;
//                break;
            default:
                break;
        }
    }else
    {
        switch (clickIndex)
        {
            case 0:
                manager.cameraLight = isOn;
                break;
            case 1:
                manager.shakeToCamera = isOn;
                break;
//            case 2:
//                manager.shakeToCamera = isOn;
//                break;
            default:
                break;
        }
    }
}


- (void)backAction{
    [[OIDSystemSettingManager shareManager] synchronizeToDisk];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
